
//
//  realPersonAuthenticationSonTabVC.m
//  BusinessMarket
//
//  Created by Michael Li on 2018/12/11.
//  Copyright © 2018年 Michael Li. All rights reserved.
//

#import "RealComplayAuthenticationSonTabVC.h"
#import "WriteRequirementTitleCell.h"
#import "TextFieldCell.h"
#import "ButtonCell.h"
#import "AuthenticationModel.h"
#import "PGDatePickManager.h"
#import "MyCollectionVC.h"
@interface realPersonAuthenticationSonTabVC ()<TextFieldCellDelegate,ButtonCellDelegate,PGDatePickerDelegate>
@property(nonatomic,strong) NSDictionary *dict;
@end
static NSString *identifierTitle = @"WriteRequirementTitleCell";
static NSString *identifierTextField = @"TextFieldCell";
static NSString *identifierBtn = @"ButtonCell";
@implementation realPersonAuthenticationSonTabVC

- (void)viewDidLoad {
    [super viewDidLoad];
    if (!self.realPerson) {
        self.navigationItem.title = @"企业实名认证";
    }else {
        self.navigationItem.title = @"个人实名认证";
    }
    [self.tableView registerNib:[UINib nibWithNibName:@"WriteRequirementTitleCell" bundle:nil] forCellReuseIdentifier:identifierTitle];
    [self.tableView registerNib:[UINib nibWithNibName:@"TextFieldCell" bundle:nil] forCellReuseIdentifier:identifierTextField];
    [self.tableView registerNib:[UINib nibWithNibName:@"ButtonCell" bundle:nil] forCellReuseIdentifier:identifierBtn];
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(certificationinsert:) name:@"certificationinsert" object:nil];
}
- (void)setShowMsg:(BOOL)showMsg
{
    _showMsg = showMsg;
    if (showMsg) {
        [self.tableView reloadData];
    }
}
#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#warning Incomplete implementation, return the number of sections
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete implementation, return the number of rows
    return 11;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    tableView.separatorStyle = UITableViewCellSelectionStyleNone;
        if (indexPath.row == 0||indexPath.row == 2||indexPath.row == 4||indexPath.row == 6||indexPath.row == 8) {
            WriteRequirementTitleCell *cell = [tableView dequeueReusableCellWithIdentifier:identifierTitle forIndexPath:indexPath];
            if (indexPath.row == 0) {
                cell.titleL.text = @"注册员工姓名";
            }else if (indexPath.row == 2) {
                cell.titleL.text = @"注册员工身份证号";
            }else if (indexPath.row == 4) {
                cell.titleL.text = @"员工担任职务";
            }else if (indexPath.row == 6) {
                cell.titleL.text = @"公司创办时间";
            }else if (indexPath.row == 8) {
                cell.titleL.text = @"公司规模大小";
            }
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            return cell;
        }else  if (indexPath.row == 1||indexPath.row == 3||indexPath.row == 5||indexPath.row == 7||indexPath.row == 9) {
            TextFieldCell *cell = [tableView dequeueReusableCellWithIdentifier:identifierTextField forIndexPath:indexPath];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            if (indexPath.row == 1) {
                cell.tf.placeholder = @"请填写注册本应用的员工的真实姓名";
                if (self.showMsg) {
                    cell.userInteractionEnabled = NO;
                    cell.tf.text = self.authModel.employeename;
                }
            }else if (indexPath.row == 3) {
                cell.tf.placeholder = @"请填写该注册员工的身份证号";
                if (self.showMsg) {
                    cell.userInteractionEnabled = NO;
                    cell.tf.text = self.authModel.employeeidnumber;
                }
            }else if (indexPath.row == 5) {
                cell.tf.placeholder = @"请填写该注册员工的公司职务名称";
                if (self.showMsg) {
                    cell.userInteractionEnabled = NO;
                    cell.tf.text = self.authModel.employeepositions;
                }
            }else if (indexPath.row == 7) {
                cell.tf.placeholder = @"请填写本公司的创办时间";
                if (self.showMsg) {
                    cell.userInteractionEnabled = NO;
                    NSTimeInterval interval    = [self.authModel.companycreatetime  integerValue]/1000.0;
                    NSDate *date               = [NSDate dateWithTimeIntervalSince1970:interval];
                    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
                    [formatter setDateFormat:@"yyyy/MM/dd"];
                    NSString *currentDateStr = [formatter stringFromDate: date];
                    cell.tf.text = currentDateStr;
                }
                cell.delegate = self;
            }else if (indexPath.row == 9) {
                cell.tf.placeholder = @"请填写本公司目前的在职人数";
                if (self.showMsg) {
                    cell.userInteractionEnabled = NO;
                    cell.tf.text = [NSString stringWithFormat:@"%ld",self.authModel.companypeople];
                }
            }
            if (indexPath.row == 7) {
                cell.tf.userInteractionEnabled = NO;
            }else {
                cell.tf.userInteractionEnabled = YES;
            }
            cell.indexPath = indexPath;
            cell.delegate = self;
            cell.tf.layer.cornerRadius = 3;
            cell.tf.layer.borderColor  = kOrangeBg.CGColor;
            cell.tf.layer.borderWidth  = 1;
            cell.lineL.hidden = YES;
            return cell;
        }else {
            ButtonCell *cell = [tableView dequeueReusableCellWithIdentifier:identifierBtn forIndexPath:indexPath];
            [cell.btn setTitle:@"提交审核" forState:UIControlStateNormal];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.delegate = self;
            if (self.showMsg) {
                cell.btn.hidden = YES;
            }
            return cell;
        }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0||indexPath.row == 2||indexPath.row == 4||indexPath.row == 6||indexPath.row == 8) {
        return 60;
    }else  if (indexPath.row == 1||indexPath.row == 3||indexPath.row == 5||indexPath.row == 7||indexPath.row == 9) {
        return 40;
    }else {
        return 400;
    }
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.row == 7) {
        PGDatePickManager *datePickManager = [[PGDatePickManager alloc]init];
        datePickManager.isShadeBackgroud = true;
        PGDatePicker *datePicker = datePickManager.datePicker;
        datePicker.delegate = self;
        datePicker.datePickerType = PGPickerViewType1;
        datePicker.isHiddenMiddleText = false;
        datePicker.datePickerMode = PGDatePickerModeDate;
        [self presentViewController:datePickManager animated:false completion:nil];
    }
}
#pragma mark-时间选择
- (void)datePicker:(PGDatePicker *)datePicker didSelectDate:(NSDateComponents *)dateComponents {
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:7 inSection:0];
    NSString *dataTime = [NSString stringWithFormat:@"%ld/%ld/%ld",(long)dateComponents.year,(long)dateComponents.month,(long)dateComponents.day];
    TextFieldCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
    cell.tf.text =dataTime;
    XLog(@"dataTime:%@",dataTime);
    self.authenticationModel.companycreatetime = dataTime;
//    self.endtime = dataTime;
//    XLog(@"dataTime:%@",dataTime);
//    NSTimeInterval time = [TimeHelper WhichTime:dataTime];
//    XLog(@"time:%.f",time);
//    self.releaseSendModel.endtime = dataTime;
}
#pragma mark-代理:公司,公司联系,社会信用代码
- (void)textFieldCell:(TextFieldCell *)textFieldCell withTextField:(UITextField *)tf withIndexPath:(NSIndexPath *)indexPath
{

    if (indexPath.row == 1) {
        self.authenticationModel.employeename = tf.text;
    }else if (indexPath.row == 3) {
        self.authenticationModel.employeeidnumber = tf.text;
    }else if (indexPath.row == 5) {
        self.authenticationModel.employeepositions = tf.text;
    }else if (indexPath.row == 7) {
        
    }else if (indexPath.row == 9) {
        self.authenticationModel.companypeople  = [tf.text integerValue];
    }
}
#pragma mark-代理---下一步
- (void)buttonCell:(ButtonCell *)buttonCell withClickBtn:(UIButton *)clickBtn
{
    NSString *companypeople = [NSString stringWithFormat:@"%ld",self.authenticationModel.companypeople];
    if (self.authenticationModel.employeename.length == 0) {
        [WSProgressHUD showImage:[UIImage imageNamed:@""] status:@"请填写注册员工姓名"];
    }else if (self.authenticationModel.employeeidnumber == 0) {
        [WSProgressHUD showImage:[UIImage imageNamed:@""] status:@"请填写注册员工身份证号"];
    }else if (self.authenticationModel.employeepositions == 0) {
        [WSProgressHUD showImage:[UIImage imageNamed:@""] status:@"请填写注册员工的职务名称"];
    }else if (self.authenticationModel.companycreatetime == 0) {
        [WSProgressHUD showImage:[UIImage imageNamed:@""] status:@"请选择公司创办时间"];
    }else if (self.authenticationModel.employeeidnumber == 0) {
        [WSProgressHUD showImage:[UIImage imageNamed:@""] status:@"请填公司规模大小"];
    }else {
        self.dict = @{
                      @"token":[UserDefaultsHelper readToken],
                      @"uid":[UserDefaultsHelper readUid],
                      @"companyname":self.authenticationModel.companyname,
                      @"mobile":self.authenticationModel.mobile,
                      @"societycreditcode":self.authenticationModel.societycreditcode,
                      @"commanyicon":self.authenticationModel.commanyicon,
                      @"idcardfront":self.authenticationModel.idcardfront,
                      @"idcardback":self.authenticationModel.idcardback,
                      @"employeepositions":self.authenticationModel.employeepositions,
                      @"employeename":self.authenticationModel.employeename,
                      @"employeeidnumber":self.authenticationModel.employeeidnumber,
                      @"companycreatetime":self.authenticationModel.companycreatetime,
                      @"companypeople":companypeople
                      };
        XLog(@"dict:%@",self.dict);
        [CustomeManager POSTWithURLIndex:42 parameters:self.dict success:^(NSDictionary *dict) {
            
        } failure:^(NSError *error) {
            
        }];
    }
}
-(void)certificationinsert:(NSNotification *)info
{
    NSDictionary *dict = info.userInfo;
    XLog(@"dict:%@",dict);
    NSString *message = [info.userInfo objectForKey:@"message"];
    [WSProgressHUD showImage:[UIImage imageNamed:@""] status:message];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        for (UIViewController *controller in self.navigationController.viewControllers) {
            if ([controller isKindOfClass:[MyCollectionVC class]]) {
                [self.navigationController popToViewController:controller animated:YES];
            }
        }
    });
}
-(void)dealloc
{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}
@end
